﻿using MediatR;
using W.Purchase.Domain;
using W.Purchase.Domain.PageDTO;
using W.Purchase.Read.API.AppLication.Command.Command;
using W.Purchase.Repository.Interface;
using static W.Purchase.Domain.PageDTO.PageInfo;

namespace W.Purchase.Read.API.AppLication.Command.CommandHandler
{
    public class ClientShowCommandHandler : IRequestHandler<ClientShowCommand, PageInfo>
    {
        private readonly IClientRepository repository;
        private readonly IClientRepository clientRepository;
        private readonly IContactRepository contactRepository;
        private readonly IClientContactRepository contactContactRepository;
        public ClientShowCommandHandler(IClientRepository repository, IClientRepository clientRepository, IContactRepository contactRepository, IClientContactRepository contactContactRepository)
        {
            this.repository = repository;
            this.clientRepository = repository;
            this.contactRepository = contactRepository; 
            this.contactContactRepository = contactContactRepository;
        }

        public async Task<PageInfo> Handle(ClientShowCommand request, CancellationToken cancellationToken)
        {
            PageInfo dto = new PageInfo();
            var list = (from a in await clientRepository.GetAllAsaync()
                       join b in await contactContactRepository.GetAllAsaync()
                       on a.ClientId equals b.ClientId
                       join c in await contactRepository.GetAllAsaync()
                       on b.ContactId equals c.ContactId
                       where b.IsDelete == false
                       select new ClientContaInfos
                       {
                           ClientId = a.ClientId,
                           ClientNo = a.ClientNo,
                           ClientName = a.ClientName,
                           ClientFax = a.ClientFax,
                           ClientPhone = a.ClientPhone,
                           ClientEmail = a.ClientEmail,
                           ClientRemark = a.ClientRemark,
                           ContactId = c.ContactId,
                           ContactName = c.ContactName,
                           ContactAddress = c.ContactAddress,
                           ContactPhone = c.ContactPhone,
                           ClientContactId = b.ClientContactId,
                           CreateTime = a.CreateTime,


                       }).ToList();
            if (!string.IsNullOrEmpty(request.ClientNo))
            {
                list = list.Where(x => x.ClientNo.Contains(request.ClientNo)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ClientName))
            {
                list = list.Where(x => x.ClientName.Contains(request.ClientName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ClientPhone))
            {
                list = list.Where(x => x.ClientPhone.Contains(request.ClientPhone)).ToList();
            }
            dto.toCount = list.Count();
            dto.pageCount = (int)Math.Ceiling(dto.toCount * 1.0 / request.pageSize);
            dto.List=list.OrderByDescending(x=>x.CreateTime).Skip((request.pageIndex-1)*request.pageSize).Take(request.pageSize).ToList();
            return dto; 
        }
    }
}
